﻿using System;
using RestSharp.Deserializers;
using System.Linq;

namespace Pogo.Data
{
   public class DirectoryEntry : PogoObject, IDirectoryEntry
   {
      internal Device ParentDevice { get; set; }
      internal Service ParentService { get; set; }

      internal string FileID { get; set; }
      internal string SpaceID { get; set; }
      internal string ParentID { get; set; }

      internal long CTime { get; set; }
      internal long MTime { get; set; }
      internal int Type { get; set; }

      [JsonName(Name = "Thumbnail")]
      internal string ThumbnailID { get; set; }

      [JsonName(Name = "Preview")]
      internal string PreviewID { get; set; }

      internal string Stream { get; set; }
      internal string Xcstamp { get; set; }
      internal string Tnstamp { get; set; }
      internal string Mdstamp { get; set; }
      internal string OrigType { get; set; }
      internal string StreamType { get; set; }
      internal string LiveStream { get; set; }
      internal string FlvStream { get; set; }

      #region IFile Members

      public string Name { get; set; }
      public string MimeType { get; set; }
      public long Size { get; set; }
      public string Filename { get; set; }

      public DateTime CreationDate
      {
         get { return new DateTime(1970, 1, 1).AddMilliseconds(CTime); }
      }

      public DateTime LastModifiedDate
      {
         get { return new DateTime(1970, 1, 1).AddMilliseconds(MTime); }
      }

      public FileType FileType
      {
         get { return (FileType) Type; }
      }

      public DirectoryEntry Preview
      {
         get
         {
            DirectoryEntry result = ParentService.GetFile(FileIdentifierType.FileID, PreviewID);
            result.Parent = this;
            return result;
         }
      }

      public DirectoryEntry Thumbnail
      {
         get
         {
            DirectoryEntry result = ParentService.GetFile(FileIdentifierType.FileID, ThumbnailID);
            result.Parent = this;
            return result;
         }
      }

      public DirectoryListing GetFiles(FileOptions options = null)
      {
         DirectoryListing result = ParentService.GetFiles(options, FileID);
         foreach (DirectoryEntry entry in result)
            entry.Parent = this;

         return result;
      }

      public byte[] Download()
      {
         throw new NotImplementedException();
      }

      public void Delete()
      {
         throw new NotImplementedException();
      }

      #endregion
   }

   public class FileOptions : ICloneable
   {
      private int _maxCount;
      private int _page;

      public FileOptions()
      {
         MaxCount = -1;
         Page = -1;
         ShowHidden = false;
         Sort = SortType.None;
         SortDirection = SortDirection.Ascending;
      }

      public int MaxCount
      {
         get { return _maxCount < 0 ? 0 : _maxCount; }
         set { _maxCount = value; }
      }

      public int Page
      {
         get { return _page < 0 ? 0 : _page; }
         set { _page = value; }
      }

      public bool ShowHidden { get; set; }
      public SortType Sort { get; set; }
      public SortDirection SortDirection { get; set; }

      public string SortString
      {
         get
         {
            switch (Sort)
            {
               case SortType.Name:
                  return SortDirection == SortDirection.Ascending ? "name+" : "name-";
               case SortType.Size:
                  return SortDirection == SortDirection.Ascending ? "size+" : "size-";
               case SortType.Type:
                  return SortDirection == SortDirection.Ascending ? "type+" : "type-";
               case SortType.Date:
                  return SortDirection == SortDirection.Ascending ? "date+" : "date-";
            }

            return null;
         }
      }

      #region ICloneable Members

      public object Clone()
      {
         FileOptions result = new FileOptions
                                 {
                                    MaxCount = MaxCount,
                                    Page = Page,
                                    ShowHidden = ShowHidden,
                                    Sort = Sort,
                                    SortDirection = SortDirection
                                 };
         return result;
      }

      #endregion
   }

   //public class FileFilter : ICloneable
   //{
   //   public string NameContains { get; set; }
   //   public Tuple<DateTime?, DateTime?> CreateDateMinMax { get; set; }
   //   public MediaType? MediaType { get; set; }

   //   public object Clone()
   //   {
   //      FileFilter result = new FileFilter
   //                             {
   //                                NameContains = NameContains,
   //                                CreateDateMinMax =
   //                                   new Tuple<DateTime?, DateTime?>(CreateDateMinMax.Item1, CreateDateMinMax.Item2),
   //                                MediaType = MediaType
   //                             };
   //      return result;
   //   }
   //}

   public enum FileType
   {
      File = 0,
      Directory = 1,
      ExtraStream = 2,
      SymbolicLink = 3
   }

   internal enum FileIdentifierType
   {
      FileID,
      Path
   }
}